<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Book;
use Illuminate\Http\Request;

class CheckBookController extends Controller
{
    /**
     * 查看所有书籍列表
     * @param int id 书籍id
     * @param string name 书籍名称
     * @param string author 作者
     * @param string publish 出版社
     * @param int pages 页数
     * @param double price 价格
     */
    public function index()
    {
        $Book = Book::paginate(10);    //分页查询
        return success_json($Book);
    }

    /**
     * 添加书籍
     * @param int id 书籍id
     * @param string name 书籍名称
     * @param string author 作者
     * @param string publish 出版社
     * @param int pages 页数
     * @param double price 价格
     */
    public function store(Request $request)
    {
        $rules = [
            'name' => 'required',
            'author' => 'required',
            'publish' => 'required',
            'pages' => 'required',
            'price' => 'required'
        ];

        $this->validate($request,$rules);

        $name = $request->input('name');
        $author = $request->input('author');
        $publish = $request->input('publish');
        $pages = $request->input('pages');
        $price = $request->input('price');

        $book = Book::where('name',$name)->first();
        if ($book) return error_json('书籍已存在');

        Book::create([
            'name' => $name,
            'author' => $author,
            'publish' => $publish,
            'pages' => $pages,
            'price' => $price
        ]);

        return success_json('添加成功');
    }

    /**
     * 查看指定书籍
     */
    public function show(Book $checkbook)
    {
        return success_json($checkbook);
    }

    /**
     * 修改书籍信息
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int id
     * @param  string name  书籍名称
     * @param  string author 作者
     * @param  string publish 出版社
     * @param  string pages 页数
     * @param  string price 价格
     */
    public function update(Request $request, Book $checkbook)
    {
        $rules = [
            'name' => 'required',
            'author' => 'required',
            'publish' => 'required',
            'pages' => 'required',
            'price' => 'required',
        ];

        $this->validate($request,$rules);

        $name = $request->input('name');
        $author = $request->input('author');
        $publish = $request->input('publish');
        $pages = $request->input('pages');
        $price = $request->input('price');

        $checkbook->update([
            'name' => $name,
            'author' => $author,
            'publish' => $publish,
            'pages' => $pages,
            'price' => $price
        ]);

        return success_json('修改成功');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy(Book $book)
    {
        $book->delete();
        return success_json('删除成功');
    }
}
